//
// posix_main.cpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include "server.hpp"
#include "log.h"

#if !defined(_WIN32)

#include <pthread.h>
#include <signal.h>

std::string address = "localhost" ;
std::string port = "20000" ;
std::size_t threadNum = 1 ;

const std::string msgQueueName = "BCP-Server2DB" ;
const std::size_t maxMsgQueueCapacity = 100 ;
const std::size_t maxMsgSize = Message::HEADER_LEN + Message::MAX_BODY_LEN ;

MsgQueuePtr pMsgQueue ;

bool buildMsgQueue()
{
    try
    {
        //Erase previous message queue
        boost::interprocess::message_queue::remove(msgQueueName.c_str());

        //Create a message_queue.
        MsgQueuePtr pMsgQ( new boost::interprocess::message_queue
                           (boost::interprocess::create_only               //only create
                            ,msgQueueName.c_str()           //name
                            ,maxMsgQueueCapacity                       //max message number
                            ,maxMsgSize               //max message size
                           ));
        pMsgQueue = pMsgQ ;
    }
    catch (boost::interprocess::interprocess_exception &ex)
    {
        std::cout << ex.what() << std::endl;
        return false;
    }

    return true ;
}
int main(int argc, char* argv[])
{
    try
    {
        // Check command line arguments.
        /*   if (argc != 5)
           {
             std::cerr << "Usage: server <address> <port> <threads> \n";
             std::cerr << "  For IPv4, try:\n";
             std::cerr << "    receiver 0.0.0.0 20000 .\n";
             std::cerr << "  For IPv6, try:\n";
             std::cerr << "    receiver 0::0 80 .\n";
             return 1;
           }
        */
        LOGCON.info(__FILE__ ", %d - Server Begins\n", __LINE__);

        buildMsgQueue() ;

        // Block all signals for background thread.
        sigset_t new_mask;
        sigfillset(&new_mask);
        sigset_t old_mask;
        pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask);

        // Run server in background thread.
        Server server ( address, port, threadNum );
        boost::thread serverThread(boost::bind(&Server::run, &server));

        // Restore previous signals.
        pthread_sigmask(SIG_SETMASK, &old_mask, 0);

        // Wait for signal indicating time to shut down.
        sigset_t wait_mask;
        sigemptyset(&wait_mask);
        sigaddset(&wait_mask, SIGINT);
        sigaddset(&wait_mask, SIGQUIT);
        sigaddset(&wait_mask, SIGTERM);
        pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
        int sig = 0;
        sigwait(&wait_mask, &sig);

        // Stop the server.
        server.stop();
        serverThread.join();
    }
    catch (std::exception& e)
    {
        std::cerr << "exception: " << e.what() << "\n";
    }

    return 0;
}

#endif // !defined(_WIN32)
